﻿$win = "디아블로 III"

HotKeySet(",", "Toggle")
HotKeySet(".", "Stop")

Global $gameDelay = 1.0
Const $pixelInTown = 0x505057
Const $pixelFind = 0xE1E1E2

;-------------------------------------------------------------------------
Opt("PixelCoordMode", 0)
Opt("MouseCoordMode", 0)

Enum $statusNone = 0, $statusMenu, $statusInTown, $statusFind, $statusGoBack, $statusDone

Global $hWnd = WinActivate($win, "")
Global $macroSize[2] = [960, 600]
Global $winPos = WinGetPos($hWnd)

Global $pause = True
Global $loopCount = 0
Global $successCount = 0
Global $hStateTimer = 0

Global $state = $statusNone
Global $nextState = $statusNone
Global $hTimer = 0
Global $step = 0

WinMove($hWnd, "", $winPos[0], $winPos[1], $macroSize[0], $macroSize[1])
changeState($statusMenu)

While True
   Sleep(100)

   If WinActive($hWnd) = 0 Then
	  Sleep(1000)
	  WinActivate($hWnd)
   EndIf

   If ($pause = True) Then
	  ContinueLoop
   EndIf

   If $nextState <> $statusNone Then
	  checkStateChange()
	  ContinueLoop
   EndIf

   Switch $state
   Case $statusMenu
	  statusMenu()
   Case $statusInTown
	  statusInTown()
   Case $statusFind
	  statusFind()
   Case $statusGoBack
	  statusGoBack()
   Case $statusDone
	  statusDone()
   EndSwitch
WEnd

Func changeState($newState)
   $nextState = $newState
   $hStateTimer = TimerInit()
EndFunc

Func checkStateChange()
   If $state <> $statusMenu And $state <> $statusDone And TimerDiff($hStateTimer) > (10000 * $gameDelay) Then
	  changeState($statusDone)
   EndIf

   Switch $nextState
   Case $statusMenu
	  Local $pixel = PixelGetColor(183, 317)
	  If $pixel <> 0x390900 Then
		 Return
	  EndIf

	  $loopCount += 1
	  ToolTip("statusMenu : " & $loopCount & " : " & $successCount, $winPos[0] + 100, $winPos[1] + 5)

   Case $statusInTown
	  Local $pixel = PixelGetColor(776, 597)
	  If $pixel <> $pixelInTown Then
		 ToolTip("statusInTown pixel : " & $pixel, $winPos[0] + 100, $winPos[1] + 5)
		 Return
	  EndIf

	  ToolTip("statusInTown : " & $loopCount & " : " & $successCount, $winPos[0] + 100, $winPos[1] + 5)

   Case $statusFind
	  Local $pixel = PixelGetColor(932, 39)
	  If $pixel <> $pixelFind Then
		 Return
	  EndIf

	  ToolTip("statusFind : " & $loopCount & " : " & $successCount, $winPos[0] + 100, $winPos[1] + 5)

	  MouseMove(338, 317)
	  MouseDown("Left")

   Case $statusGoBack
	  MouseMove(394, 235)
	  MouseDown("Left")

	  ToolTip("statusGoBack : " & $loopCount & " : " & $successCount, $winPos[0] + 100, $winPos[1] + 5)

   Case $statusDone
	  Local $pixel = PixelGetColor(790, 70)
	  If $pixel <> 0xB00639 Then
		 Send("{ESC}")
		 Sleep(500 * $gameDelay)
		 Return
	  EndIf

	  ToolTip("statusDone : " & $loopCount & " : " & $successCount, $winPos[0] + 100, $winPos[1] + 5)

   EndSwitch

   $state = $nextState
   $nextState = $statusNone
   $hTimer = TimerInit()
   $step = 0
EndFunc

Func statusMenu()
   Sleep(500 * $gameDelay)
   MouseClick("left", 183, 317)

   changeState($statusInTown)
EndFunc

Func statusInTown()
   Sleep(500 * $gameDelay)
   MouseClick("left", 21, 50)
   Sleep(3000)

   changeState($statusFind)

EndFunc

Func statusFind()
   Switch $step
   Case 0
	  If TimerDiff($hTimer) >= 2500 Then
		 MouseMove(470, 180)
		 $hTimer = TimerInit()
		 $step = 1
	  EndIf
   Case 1
	  If TimerDiff($hTimer) >= 1000 Then
		 MouseMove(600, 200)
		 $hTimer = TimerInit()
		 $step = 2
	  EndIf
   Case 2
	  If TimerDiff($hTimer) >= 1000 Then
		 MouseMove(700, 200)
		 $hTimer = TimerInit()
		 $step = 3
	  EndIf
   Case 3
	  If TimerDiff($hTimer) >= 1500 Then
		 MouseMove(598, 400)
		 $hTimer = TimerInit()
		 $step = 4
	  EndIf
   Case 4
	  If TimerDiff($hTimer) >= 5500 Then
		 MouseUp("left")
		 Send("t")
		 Sleep(7000)
		 changeState($statusDone)
	  EndIf
   EndSwitch

   Local $pos = PixelSearch(250 , 150, 700, 450, 0xF79F02, 10)
   If Not @error Then
	  MouseClick("left", $pos[0], $pos[1] + 50, 2, 0)
	  Sleep(1500)
	  changeState($statusGoBack)
   EndIf

EndFunc

Func statusGoBack()
   Switch $step
   Case 0
	  If TimerDiff($hTimer) >= 5500 Then
		 MouseMove(320, 394)
		 $hTimer = TimerInit()
		 $step = 1
	  EndIf
   Case 1
	  If TimerDiff($hTimer) >= 2500 Then
		 MouseMove(490, 405)
		 $hTimer = TimerInit()
		 $step = 2
	  EndIf
   Case 2
	  If TimerDiff($hTimer) >= 1500 Then
		 MouseMove(580, 366)
		 $hTimer = TimerInit()
		 $step = 3
	  EndIf
   Case 3
	  If TimerDiff($hTimer) >= 1500 Then
		 MouseMove(587, 261)
		 $hTimer = TimerInit()
		 $step = 4
	  EndIf
   Case 4
	  If TimerDiff($hTimer) >= 2000 Then
		 MouseUp("left")
		 Send("t")
		 Sleep(7000)
		 changeState($statusDone)
	  EndIf
   EndSwitch

   Local $pos = PixelSearch(250 , 150, 700, 450, 0xF79F02, 10)
   If Not @error Then
	  MouseClick("left", $pos[0], $pos[1] + 50, 2, 0)
	  Sleep(1500)
	  $successCount += 1
	  Send("t")
	  Sleep(7000)
	  changeState($statusDone)
   EndIf
EndFunc

Func statusDone()
   MouseClick("left", 172, 298)
   Sleep(1000)

   MouseMove(100, 100)
   Sleep(1000)

   changeState($statusMenu)
EndFunc

Func Toggle()
   If $pause = True Then
	  $pause = False
	  ToolTip("Resume : " & $loopCount & " : " & $successCount, $winPos[0] + 100, $winPos[1] + 5)
   Else
	  $pause = True
	  ToolTip("Pause : " & $loopCount & " : " & $successCount, $winPos[0] + 100, $winPos[1] + 5)
   EndIf
EndFunc

Func Stop()
   ToolTip("Stop : " & $loopCount & " : " & $successCount, $winPos[0] + 100, $winPos[1] + 5)
   Sleep(2000)
   Exit
EndFunc
